package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.ScheduleData;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by Megan on 16/3/16.
 */
public class GetScheduleMsg extends CcMsgStructure {

    private static final String TAG = "GetScheduleMsg";

    private Message mmCallback;
    private Context mContext;
    private CloudDBOperation cloudDBOperation;

    public GetScheduleMsg(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
        cloudDBOperation = new CloudDBOperation(mContext);
    }

    @Override
    public void onReceive(JSONObject data) {
        ScheduleData scheduleData = new ScheduleData();

        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            scheduleData.setTitle(jsonObject.getString(AsynHttpClient.KEY_EVENT_TITLE));
            scheduleData.setId(jsonObject.getString(AsynHttpClient.KEY_EVENT_ID));
            scheduleData.setPublishedUId(jsonObject.getString("published_user_id"));
        } catch (JSONException e) {
            Log.d(TAG,e.toString());
            e.printStackTrace();
        }

        if (mmCallback != null)

        {
            mmCallback.obj = scheduleData;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
